<template>
  <div class="app-container">
    <!-- 添加或修改文章管理对话框 -->
    <el-form ref="form" :model="form" :rules="rules" label-width="120px">
      <el-form-item label="寺院简介标题" prop="title">
        <el-input v-model="form.title" placeholder="请输入文章标题" />
      </el-form-item>
      <!--
      <el-form-item label="文章封面" prop="poster">
        <imageUpload v-model="form.poster" />
      </el-form-item> -->
      <!-- <el-form-item label="文章描述" prop="description">
          <el-input v-model="form.description" placeholder="请输入文章描述" />
        </el-form-item> -->
      <el-form-item label="寺院简介内容" prop="content">
        <TinymceEditor v-model="form.content" />
      </el-form-item>
      <!-- <el-form-item label="删除标志" prop="delFlag">
          <el-input v-model="form.delFlag" placeholder="请输入删除标志" />
        </el-form-item> -->

      <!-- <el-divider content-position="center">文章分类信息</el-divider>
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button type="primary" icon="el-icon-plus" size="mini" @click="handleAddArticleCategory">添加</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="danger" icon="el-icon-delete" size="mini" @click="handleDeleteArticleCategory">删除</el-button>
          </el-col>
        </el-row>
        <el-table :data="articleCategoryList" :row-class-name="rowArticleCategoryIndex" @selection-change="handleArticleCategorySelectionChange" ref="articleCategory">
          <el-table-column type="selection" width="50" align="center" />
          <el-table-column label="序号" align="center" prop="index" width="50"/>
          <el-table-column label="分类名称" prop="name">
            <template slot-scope="scope">
              <el-input v-model="scope.row.name" placeholder="请输入分类名称" />
            </template>
          </el-table-column>
          <el-table-column label="分类图标" prop="icon">
            <template slot-scope="scope">
              <el-input v-model="scope.row.icon" placeholder="请输入分类图标" />
            </template>
          </el-table-column>
          <el-table-column label="排序" prop="sort">
            <template slot-scope="scope">
              <el-input v-model="scope.row.sort" placeholder="请输入排序" />
            </template>
          </el-table-column>
        </el-table> -->
    </el-form>
    <div slot="footer" class="dialog-footer" style="text-align:center;">
      <el-button type="primary" @click="submitForm">确 认 修 改</el-button>
      <!-- <el-button @click="cancel">取 消</el-button> -->
    </div>
  </div>
</template>

<script>
import {
  listArticle,
  getArticle,
  delArticle,
  addArticle,
  updateArticle,
  exportArticle
} from '@/api/mall/article'

import { listArticleCategory } from '@/api/mall/articleCategory'
// import Editor from '@/components/Editor'
import TinymceEditor from '@/components/tinymce-editor'

export default {
  name: 'Article',
  components: {
    TinymceEditor
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 子表选中数据
      checkedArticleCategory: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 文章管理表格数据
      articleList: [],
      // 文章分类表格数据
      articleCategoryList: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        title: null,
        description: null,
        content: null,
        categoryId: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        title: [
          { required: true, message: '文章标题不能为空', trigger: 'blur' }
        ],
        categoryId: [
          { required: true, message: '文章分类不能为空', trigger: 'blur' }
        ],
        poster: [
          { required: true, message: '文章内容不能为空', trigger: 'blur' }
        ],
        content: [
          { required: true, message: '文章内容不能为空', trigger: 'blur' }
        ]

        // createBy: [
        //   { required: true, message: '创建者不能为空', trigger: 'blur' }
        // ],
        // createTime: [
        //   { required: true, message: '创建时间不能为空', trigger: 'blur' }
        // ],
        // updateTime: [
        //   { required: true, message: '更新时间不能为空', trigger: 'blur' }
        // ],
        // updateBy: [
        //   { required: true, message: '更新者不能为空', trigger: 'blur' }
        // ],
        // delFlag: [
        //   { required: true, message: '删除标志不能为空', trigger: 'blur' }
        // ],
      }
    }
  },
  created() {
    getArticle(1).then(res => {
      this.form = res.data
    })
  },
  methods: {
    /** 查询文章管理列表 */
    getList() {
      this.loading = true
      listArticle(this.queryParams).then(response => {
        this.articleList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        title: null,
        description: null,
        content: null,
        createBy: null,
        createTime: null,
        updateTime: null,
        updateBy: null,
        delFlag: null,
        categoryId: null
      }
      // this.articleCategoryList = []
      this.resetForm('form')
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.title = '添加文章管理'
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      const id = row.id || this.ids
      getArticle(id).then(response => {
        this.form = response.data
        // this.articleCategoryList = response.data.articleCategoryList
        this.open = true
        this.title = '修改文章管理'
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          this.form.articleCategoryList = undefined
          // this.articleCategoryList
          if (this.form.id != null) {
            updateArticle(this.form).then(response => {
              this.msgSuccess('修改成功')
              this.open = false
              this.getList()
            })
          } else {
            addArticle(this.form).then(response => {
              this.msgSuccess('新增成功')
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids
      this.$confirm(
        '是否确认删除文章管理编号为"' + ids + '"的数据项?',
        '警告',
        {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }
      )
        .then(function() {
          return delArticle(ids)
        })
        .then(() => {
          this.getList()
          this.msgSuccess('删除成功')
        })
    },
    /** 文章分类序号 */
    rowArticleCategoryIndex({ row, rowIndex }) {
      row.index = rowIndex + 1
    },
    /** 文章分类添加按钮操作 */
    handleAddArticleCategory() {
      const obj = {}
      obj.name = ''
      obj.icon = ''
      obj.sort = ''
      this.articleCategoryList.push(obj)
    },
    /** 文章分类删除按钮操作 */
    handleDeleteArticleCategory() {
      if (this.checkedArticleCategory.length == 0) {
        this.$alert('请先选择要删除的文章分类数据', '提示', {
          confirmButtonText: '确定'
        })
      } else {
        this.articleCategoryList.splice(
          this.checkedArticleCategory[0].index - 1,
          1
        )
      }
    },
    /** 单选框选中数据 */
    handleArticleCategorySelectionChange(selection) {
      if (selection.length > 1) {
        this.$refs.articleCategory.clearSelection()
        this.$refs.articleCategory.toggleRowSelection(selection.pop())
      } else {
        this.checkedArticleCategory = selection
      }
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams
      this.$confirm('是否确认导出所有文章管理数据项?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(function() {
          return exportArticle(queryParams)
        })
        .then(response => {
          this.download(response.msg)
        })
    }
  }
}
</script>
